import 'dart:async';

import 'package:flutter_wan_android/api/api_server.dart';
import 'package:flutter_wan_android/bean/article_list_bean.dart';
import 'package:flutter_wan_android/bean/banner_bean.dart';
import 'package:flutter_wan_android/bean/home_data.dart';
import 'package:flutter_wan_android/bean/project_list_bean.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:rxdart/rxdart.dart';
import 'package:rxdart/rxdart.dart' as prefix0;

class HomeBloc extends BaseBloc {
  BehaviorSubject<List<BannerBean>> _bannerSubject = BehaviorSubject();

  Stream<List<BannerBean>> get bannerStream => _bannerSubject.stream;



  BehaviorSubject<List<ArticleItem>> _articleSubject = BehaviorSubject();

  Stream<List<ArticleItem>> get articleStream => _articleSubject.stream;

  BehaviorSubject<HomeData> _homeSubject = BehaviorSubject();

  Stream<HomeData> get homeStream => _homeSubject.stream;

  Future getBanner() async {
    return await ApiServer.getBannerData()
        .then((list) => _bannerSubject.sink.add(list))
        .catchError((error) => _bannerSubject.sink.addError(error));
  }



  Future getHomeArticleList(pageIndex) async {
    return await ApiServer.getArticleList(pageIndex)
        .then((article) => _articleSubject.sink.add(article.datas))
        .catchError((error) => _articleSubject.sink.addError(error));
  }




  Future getHomeData(pageIndex) async {
    Observable<List<BannerBean>> bannerObservable =
        Observable<List<BannerBean>>.fromFuture(ApiServer.getBannerData());
    Observable<ProjectListBean> projectObservable =
        Observable<ProjectListBean>.fromFuture(
            ApiServer.getHomeProjectList(pageIndex));
    Observable<ArticleListBean> articleObservable =
        Observable<ArticleListBean>.fromFuture(ApiServer.getArticleList(0));
    return Observable.zip3<List<BannerBean>, ProjectListBean, ArticleListBean,
            HomeData>(
        bannerObservable,
        projectObservable,
        articleObservable,
        (list1, project, article) =>
            HomeData(list1, project.datas, article.datas)).listen((data) {
      _homeSubject.sink.add(data);
    }, onError: (error) {
      _homeSubject.sink.addError(error);
    });
  }

  @override
  void dispose() {
    _bannerSubject.close();
    _articleSubject.close();
    _homeSubject.close();
  }






}
